package com.kly.cashmall.module.order.adapter;

import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kly.cashmall.R;
import com.kly.cashmall.bean.LoanDetailBillVOSBean;
import com.kly.cashmall.utils.data.TextUtil;

/**
 * Created by
 * on 2019/5/13.
 */
public class LoanPlanOfRepayAdapter extends BaseQuickAdapter<LoanDetailBillVOSBean, BaseViewHolder> {

    private OnClickListener clickListener;

    public LoanPlanOfRepayAdapter(OnClickListener clickListener) {
        super(R.layout.item_plan_repay);
        this.clickListener = clickListener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, LoanDetailBillVOSBean billVOS) {
        SpannableString string = new SpannableString(billVOS.getRepaymentAmount());
        if (billVOS.getRepaymentAmount().contains("₹")){
            string.setSpan(new RelativeSizeSpan(0.78f), billVOS.getRepaymentAmount().indexOf("₹"), billVOS.getRepaymentAmount().indexOf("₹") + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        holder.setText(R.id.item_amount,string);
        holder.setText(R.id.item_number, String.valueOf(billVOS.getCurrentPeriod()));
        holder.setText(R.id.item_pricipal, "Principal: " + billVOS.getPrincipal());
        holder.setText(R.id.item_interest, "interest: " + billVOS.getInterest());
        if (TextUtil.isEmpty(billVOS.getOverdueFee())){
            holder.setVisible(R.id.item_overdue_fee,false);
        }else {
            holder.setVisible(R.id.item_overdue_fee,true);
            holder.setText(R.id.item_overdue_fee, "overdueFee: " + billVOS.getOverdueFee());
        }
        holder.setText(R.id.item_date, getContext().getString(R.string.repay_date) + billVOS.getRepaymentDate());
        TextView mItemRepay = holder.getView(R.id.item_repay);
        ImageView itemRepayArrow = holder.getView(R.id.item_repay_arrow);
        if (!billVOS.isPaid()) {
            holder.setText(R.id.item_repay, billVOS.getButtonText());
            mItemRepay.setTextColor(getContext().getResources().getColor(R.color.color_FF5621));
            itemRepayArrow.setImageResource(R.mipmap.ic_right_red_arrow_double);
        } else {
            holder.setText(R.id.item_repay, billVOS.getButtonText());
            mItemRepay.setTextColor(getContext().getResources().getColor(R.color.color_black_two));
            itemRepayArrow.setImageResource(R.mipmap.ic_right_arrow_double);
        }

        holder.getView(R.id.item_repay).setOnClickListener(view -> {
            clickListener.clickListener(view, billVOS);
        });

        holder.setVisible(R.id.item_line, holder.getAdapterPosition() + 1 != getItemCount());
    }

    public interface OnClickListener {
        void clickListener(View view, LoanDetailBillVOSBean billVOS);
    }
}
